//
//  ProcurementView.m
//  wedding
//
//  Created by 常宽 on 15/1/24.
//  Copyright (c) 2015年 常宽. All rights reserved.
//
/*********采购页面**********/
#import "ProcurementView.h"
#import "PVItemCell.h"      /// 受宠单品cell
#import "ItemModel.h"       /// 受宠单品model
#import "PVThemeCell.h"     /// 主题cell
#import "ThemeModel.h"      /// 主题model
#import "PVGoodsCell.h"     /// 推荐商品cell
#import "GoodsModel.h"      /// 推荐商品model
#import "SectionrHeaderView.h"//自定义section

@interface ProcurementView ()<UITableViewDataSource, UITableViewDelegate, PullTableViewDelegate>
/// 刷新加载的表视图
@property (nonatomic, retain) CKTableView *tableView;
/// 受宠单品
@property (nonatomic, retain) NSMutableArray *itemArray;
/// 主题
@property (nonatomic, retain) NSMutableArray *themeArray;
/// 推荐商品
@property (nonatomic, retain) NSMutableArray *goodsArray;

@end

@implementation ProcurementView

- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.
        self.itemArray = [[NSMutableArray alloc] init];
        self.themeArray = [[NSMutableArray alloc] init];
        self.goodsArray = [[NSMutableArray alloc] init];
        [self netWorking];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self createTableView];
}

- (void)netWorking
{
    // 受宠单品
    NSString *strUrl0 = @"http://hunliji.com/v2/api/app/banners.json?site=3";
    [NetHandler networkWithURL:strUrl0 completion:^(id result) {
        for (NSDictionary *dic in result) {
            ItemModel *model = [[ItemModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.itemArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    
    // 主题
    NSString *strUrl1 = @"http://hunliji.com/v1/api/app/newsubjects.json?page=1&per_page=3";
    [NetHandler networkWithURL:strUrl1 completion:^(id result) {
        NSArray *array = [result objectForKey:@"subjects"];
        for (NSDictionary *dic in array) {
            ThemeModel *model = [[ThemeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.themeArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    
    // 推荐商品
    NSString *strUrl2 = @"http://hunliji.com/v1/api/app/products.json?page=1&per_page=10";
    [NetHandler networkWithURL:strUrl2 completion:^(id result) {
        NSArray *array = [result objectForKey:@"products"];
        for (NSDictionary *dic in array) {
            GoodsModel *model = [[GoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.goodsArray addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)createTableView
{
    self.tableView = [[CKTableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    [self addSubview:self.tableView];
}
#pragma mark 加载刷新
- (void) refreshTable
{
    [self.tableView reloadData];
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
//    //  加载更多实现代码放在在这
    [self shuaxinNetWork];
    self.tableView.pullTableIsLoadingMore = NO;
}
/// 加载更多
///  刷新方法
static NSInteger count = 2;
- (void)shuaxinNetWork
{
    NSString *hotUrl = [NSString stringWithFormat:@"http://hunliji.com/v1/api/app/products.json?page=%ld&per_page=10", count];
    [NetHandler networkWithURL:hotUrl completion:^(id result) {
        NSArray *array = [result objectForKey:@"products"];
        for (NSDictionary *dic in array) {
            GoodsModel *model = [[GoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.goodsArray addObject:model];
        }
        [self.tableView reloadData];
    }];
    count ++;
    
    //    NSLog(@"%@",hotUrl);
}
#pragma mark -
#pragma mark - PullTableViewDelegate协议

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{    
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return self.goodsArray.count;
            break;
        default:
            return 0;
            break;
    }
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0://受宠单品
        {
            static NSString *string0 = @"Item";
            PVItemCell *cell = [tableView dequeueReusableCellWithIdentifier:string0];
            if (!cell) {
                cell = [[PVItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string0];
            }
            cell.model = [self.itemArray lastObject];
            return cell;
        }
            break;
        case 1://主题
        {
            static NSString *string1 = @"Theme";
            PVThemeCell *cell = [tableView dequeueReusableCellWithIdentifier:string1];
            if (!cell) {
                cell = [[PVThemeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string1];
            }
            cell.array = self.themeArray;
            cell.block = ^(ThemeModel *model){
                self.themeBlock(model);
            };
            return cell;
        }
            break;
        case 2://推荐商品
        {
            static NSString *string2 = @"Goods";
            PVGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:string2];
            if (!cell) {
                cell = [[PVGoodsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string2];
            }
            cell.model = [self.goodsArray objectAtIndex:indexPath.row];
            return cell;
        }
            break;
        default:
        {
            static NSString *string = @"aaa";
            BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
            if (!cell) {
                cell = [[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
            }
            return cell;
        }
            break;
    }
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return self.frame.size.width * 0.4;
        }
            break;
        case 1:
        {
            return self.frame.size.width * 0.4;
        }
            break;
        case 2:
        {
            GoodsModel *model = [self.goodsArray objectAtIndex:indexPath.row];
            float w = [model.width floatValue];
            float h = [model.height floatValue];
            float H = (tableView.frame.size.width / w) * h;
            return H;           
        }
            break;
        default:
            return 0;
            break;
    }
}
///  section 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 分区的高度
    switch (section) {
//        case 0:
//            return 25;
//            break;
        case 1:
            return 25;
            break;
        case 2:
            return 25;
            break;
        default:
            return 0;
            break;
    }
}

///  返回自定义sectionView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionrHeaderView *sectionView = [[SectionrHeaderView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 15)];
    sectionView.layer.cornerRadius = 4.f;
    switch (section) {
//        case 0:
//        {
//            sectionView.title.text = @"受宠单品";
//        }
//            break;
        case 1:
        {
            sectionView.title.text = @"主题";
        }
            break;
        case 2:
        {
            sectionView.title.text = @"商品推荐";
        }
            break;
            
        default:
            break;
    }
    sectionView.tiaoZhuanBlock = ^(NSString *title){
        if ([title isEqualToString:@"主题"]) {
            /// 更多主题
            self.themeMoreBlock();
        } else if ([title isEqualToString:@"商品推荐"]){
            /// 更多商品推荐
            self.goodsMoreBlock();
        }

    };
    return sectionView;
}

///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            self.itemBlock([self.itemArray lastObject]);
        }
            break;
        case 2:
        {
            self.goodsBlock([self.goodsArray objectAtIndex:indexPath.row]);
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
